#!/usr/bin/env bash

# output format will be like this
# country year indicate_code(48 item)
# year 1960~2015
# read 48 line, then process
# country_name=$1
# year=((1960 ~))

let SIZE=48
let BASE_YEAR=1960
let BASE_YEAR_COLUMN=5
let MAX_YEAR=2015

##########
cat /dev/null > _tmp_file1_ # record one country's data
cat /dev/null > output.csv  # csv for mysql import

# cat /dev/null > log
# exec  1>log
# cat /dev/null > errlog
# exec  2>errlog
##########

# bug 当 csv中 包含"aa,bb",cc时，无法正确解析  ==> csvtool , need install
# 解决：使用 csvtool
# http://stackoverflow.com/questions/1560393/bash-shell-scripting-csv-parsing

country_name=
function check_data(){
  # step0. check all same country
  country_name=`head -n1 _tmp_file1_ | csvtool -t ',' col 1 - | grep "$value"`
  local size=`grep "$country_name" _tmp_file1_ | wc -l`
  # echo $country_name $size
  if [ $size -ne $SIZE ]; then
    echo "process wrong,please check _tmp_file1_"
    exit 1
  fi
}


function process_one_country() {
  # step1. do process
  echo now process country: "$country_name"
  let i=0
  let row_cnt=$BASE_YEAR_COLUMN
  let year=$BASE_YEAR
  let col=$BASE_YEAR_COLUMN
  for (( i=$BASE_YEAR; i<=$MAX_YEAR; i++ )); do
    let j=0
    one_row="$year,$country_name,"
    cat _tmp_file1_ | csvtool -t ',' col $row_cnt - | grep "$value" | while read _line; do
      ((j++))
      if [ $j -eq $SIZE ]; then
        one_row=$one_row""$_line
        echo $one_row >> output.csv
      else
        one_row=$one_row""$_line","
      fi
    done

    ####
    ((row_cnt++))
    ((year++))
    ((col++))
  done

  # step2. after process
  cat /dev/null > _tmp_file1_
}



#
let count=0
cat "API_1_DS2_en_csv_v2.csv" | while read line; do
  echo $line >> _tmp_file1_
  ((count++))
  if [ $count -eq $SIZE ]; then
    check_data
    process_one_country
    # exit 1  # debug one country
    count=0
  fi
done

## all done without error
rm errlog log _tmp_file1_
